<?php
namespace Admin\Controller;
use Think\Controller;
class RecordController extends Controller {
    protected function _initialize() {
        $system['name']="交费系统";
        $this->assign('system',$system);
        $crumbsMap['RecordCheck'] = '查询'; 
        $this->assign('crumbsMap', $crumbsMap);
    }
    public function RecordCheck(){
        $this->assign('modelIcon', 'icon-usd');
        $this->assign('modelName', '交费');
        $header = array(
            'sup'=>array(
                'name'=>'缴费记录管理',
                'record_check'=>'active',
            ),
            'sub'=>array(
                'name'=>'查询学生信息',
                'record_check'=>'active',
            ),
            'icon'=>'icon-usd',
            'url'=>'RecordCheck',
        );
        $this->assign('header', $header);
        $this->display('record_check');
    }

    public function RecordStudent($name='',$identity='') {
        $this->assign('modelIcon', 'icon-user');
        $crumbsMap['RecordStudent'] = '学生信息'; 
        $this->assign('crumbsMap', $crumbsMap);
        $this->assign('modelName', '交费');
        unset($data);
        if ($name != '') {
            $data['name'] = ['LIKE', '%'.$name.'%'];
        }
        if ($identity != '') {
            $data['identity'] = ['LIKE', '%'.$identity.'%'];
        }
        $student=D('student')->where($data)->find();
        if(!$student){
            $this->error('该学生不存在',U('Record/RecordCheck'));
        }
        $this->assign('student',$student);
        $class=D('Class')->where(['id'=>$student['class_id']])->find();
        $this->assign('class',$class);
        $grade=D('grade')->where(['id'=>$class['grade_id']])->find();
        $this->assign('grade',$grade);
        $major=D('major')->where(['id'=>$class['major_id']])->find();
        $this->assign('major',$major);
        $this->display('record_student');
    }  

    public function RecordR($identity='',$update_timeStart ='',$update_timeEnd ='',$water = '',$term_id = '',$student_id='',$way='',$term_name='') {
        $this->assign('modelIcon', 'icon-user');
        $crumbsMap['RecordR'] = '交费记录'; 
        $this->assign('crumbsMap', $crumbsMap);
          $header = array(
            'sup'=>array(
                'name'=>'缴费记录管理',
                'record'=>'active',
            ),
            'sub'=>array(
                'name'=>'缴费记录列表',
                'record_r'=>'active',
            ),
            'icon'=>'icon-usd',
            'url'=>'RecordR',
        );
        $this->assign('header', $header);
         $findFlag = 0;
        
        if (($identity!= '')&&($findFlag == 0)) {
            $student = M('Student')->where(['identity'=>$identity])->find();
            $findMap['student_id'] = ['eq', $student['id']];
            $findFlag = 1;

        }
        if($term_name!=''){
            $terms = D('Term')->where(['name'=>['LIKE', '%'.$term_name.'%']])->select();
            $data=[];
            foreach($terms as $term){
                $tuition=D('Tuition')->where(['term_id'=>$term['id']])->select();
                foreach ($tuition as $value){
                    $data[]=$value['id'];
                }
            }
            if(count($data)!=0){
                $findMap['tuition_id']=['IN',$data];
            }
        }

        
        if (($student_id!= '')&&($findFlag == 0)) {
            $findMap['student_id'] = ['LIKE', '%'.$student_id.'%'];
        }

        if (($water!= '')&&($findFlag == 0)) {
            $findMap['water'] = ['LIKE', '%'.$water.'%'];
        }
        if (($way!= '')&&($findFlag == 0)) {
            $findMap['way'] = ['LIKE', '%'.$way.'%'];
        }
        if($update_timeStart !='' && $update_timeEnd !=''){
            $findMap['time'] = ['between', [strtotime($update_timeStart), strtotime($update_timeEnd)]];
        }
       //===分页获取信息======
        $model = M('Record');
        $count = $model->where($findMap)->count();
        $page = new \Think\AdminPage($count,C('PAGE_NUM'));
        $show = $page->show();
        $list = $model->where($findMap)->order('id')->limit($page->firstRow.','.$page->listRows)->select();
        $this->assign('list',$list);

        // 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $student = M('Student')->select();
        $this->assign('student', $student);
        
        $grade = M('Grade')->select();
        $this->assign('grade', $grade);
       
        $term = M('Term')->select();
        $this->assign('term', $term);
        $major = M('Major')->select();
        $this->assign('major', $major);
        $this->display('record_r');
    }
    public function RecordPayment(){
        $this->assign('modelIcon', 'icon-user');
        $crumbsMap['RecordPayment'] = '详情'; 
        $this->assign('crumbsMap', $crumbsMap);
        $this->assign('modelName', '交费');
        //找到这个学生
        $id=I('get.id');
        //找到这个学生
        $student = D('Student')->find($id);
        $this->assign('student',$student);
        //找到这个学生应交的学费项
        $fee=D('fee')->where(['student_id'=>$id])->select();
        $this->assign('fee',$fee);
        if(IS_POST){
            foreach ($_POST['over'] as $key=>$value) {
                $temp = D('fee')->where(['student_id'=>$id,'tuition_id'=>$key])->find();
                if ($_POST['should'][$key]<$value+$temp['over']) {
                    $this->error('实交费用不能高出应交费用，请检查!');
                }
            }
            $_SESSION['recordids'] = [];
            $tem=0;
            foreach ($_POST['over'] as $key=>$value) {
                $tem=$tem+$value;
            }
            if($tem==0){
                $this->error('交费金额不能为空');
                    
            }
            foreach ($_POST['over'] as $key=>$value) {
                if($value!=0){
                unset($data);
                $temp =D('fee')->where(['student_id'=>$id,'tuition_id'=>$key])->find();
                $data['over'] = $value+$temp['over'];
                $data['water'] = time().rand(1000,9999);
                $data['update_time'] = time();
                $data['name'] =I('post.name');
                $data['way']=I('post.way');
                D('fee')->where(['student_id'=>$id,'tuition_id'=>$key])->data($data)->save();
                $tuition=D('Tuition')->find($key);
                $term =D('term')->where(['id'=>$tuition['term_id']])->find();
                //增加缴费记录
                unset($map);
                $map['water']=time().rand(1000,9999);
                $map['money']=$value;
                $map['name']=I('post.name');
                $map['student_id']=$id;
                $map['term_id']=$term['id'];
                $map['time']=time();
                $map['way']=I('post.way');
                $newId = D('Record')->data($map)->add();
                $_SESSION['recordids'][] = $newId;

                }
            }
           $this->success('交费成功!', U('Record/RecordSelect',['student_id'=>$id]));
        }
        //该学生应交的总金额
        $class=D('Class')->where(['id'=>$student['class_id']])->find();
        $grade=D('grade')->where(['id'=>$class['grade_id']])->find();
        $major=D('major')->where(['id'=>$class['major_id']])->find();
        $tuition=D('Tuition')->where(['grade_id'=>$grade['id'],'major_id'=>$major['id']])->select();
        $shouldMun=0;
        foreach($tuition as $k=>$v){
           $shouldModel=D('should')->where(['tuition_id'=>$v['id'],'student_id'=>$id])->find();
            if($shouldModel){
              $shouldMun=$shouldMun+ $shouldModel['should'];
            }else{
                $shouldMun=$shouldMun+$v['money'];
            }
        }
        $this->assign('shouldMun',$shouldMun);
        //实交总金额
        $overSum=D('fee')->field('sum(over) as sum')->where(['student_id'=>$id])->select();
        $overSum = $overSum[0]['sum'];
        $this->assign('overSum', $overSum);
 
        $this->display('record_payment');
    }
    public function RecordSelect(){
        $this->assign('modelIcon', 'icon-th');
        $crumbsMap['RecordSelect'] = '选择'; 
        $this->assign('crumbsMap', $crumbsMap);
        $this->assign('modelName', '交费');
        //找到这个学的id
        $student_id=I('get.student_id');
        //找到这个学生
        $student=D('Student')->find($student_id);
        $this->assign('student',$student);
        //找到这个学生交费项
        $fee=D('Fee')->where(['student_id'=>$student_id])->select();
        $this->assign('fee',$fee);
        //交费记录
        $record = D('Record')->where(['id'=>['IN', $_SESSION['recordids']]])->select();
        $this->assign('record', $record);
        $this->display('record_select');
    }
    public function Print($student_id = 0, $recordidStr = ''){
            $recordidStr = trim($recordidStr, ',');
            $recordidArr = explode(',', $recordidStr);
            if ($recordidArr[0] ==='') {
                $this->error('至少选择一个交费项进行打印!');
            }
            //需要打印的item
            // $feeArr = [];
            // foreach ($termArr as $term_id) {
            //     $feeArr[] = D('Fee')->where(['student_id'=>$student_id, 'term_id'=>$term_id])->find();
            // }
            $recordArr = D('Record')->where(['id'=>['IN', $recordidArr]])->select();
            $this->assign('recordArr', $recordArr);
            $student = D('Student')->find($student_id);
            $class=D('class')->where(['id'=>$student['class_id']])->find();
            $this->assign('class',$class);
            $grade=D('grade')->where(['id'=>$class['grade_id']])->find();
            $this->assign('grade',$grade);
            $major=D('major')->where(['id'=>$class['major_id']])->find();
            $this->assign('major',$major);
            $this->assign('student',$student);
            $this->display('print');
    }
    public function RecordLook(){
        $this->assign('modelIcon', 'icon-th');
        $crumbsMap['RecordLook'] = '查询'; 
        $this->assign('crumbsMap', $crumbsMap);
        $this->assign('modelName', '交费');
           $header = array(
            'sup'=>array(
                'name'=>'缴费记录管理',
                'record'=>'active',
            ),
            'sub'=>array(
                'name'=>'查询缴费信息',
                'record_look'=>'active',
            ),
            'icon'=>'icon-usd',
            'url'=>'RecordLook',
        );
        $this->assign('header', $header);
        $this->display('record_look');

    }
    public function RecordMake($name='',$identity=''){
       $this->assign('modelIcon', 'icon-th');
        $crumbsMap['RecordMake'] = '详情'; 
        $this->assign('crumbsMap', $crumbsMap);
        $this->assign('modelName', '交费');
        if ($name != '') {
            $findMap['name'] = ['LIKE', '%'.$name.'%'];
        }
        if ($identity != '') {
            $findMap['identity'] = ['LIKE', '%'.$identity.'%'];
        }
        //找到这个学生
        $model=D('Student')->where($findMap)->find();
        $this->assign('model',$model);
        if(!$model){
            $this->error('该学生不存在',U('Record/RecordLook'));
        }
        //找到这个学生的缴费情况
        $fee= D('fee')->where(['student_id'=>$model['id']])->select();
       /* if(!$fee){
            $this->error('该学生没有交费项');
        }*/
        $this->assign('fee',$fee);
        //找到这个学生的缴费记录
        $record=D('Record')->where(['student_id'=>$model['id']])->select();
        $this->assign('record',$record);
        $this->display('record_make');
    }
     public function RecordOption(){
        $this->assign('modelIcon', 'icon-th');
        $crumbsMap['RecordOption'] = '选择'; 
        $this->assign('crumbsMap', $crumbsMap);
        $this->assign('modelName', '交费');
        $id=I('get.student_id');
        //找到这个学生
        $student=D('Student')->find($id);
        $this->assign('student',$student);
        $record=D('Record')->where(['student_id'=>$student['id']])->select();
        $this->assign('record', $record);
        $this->display('record_option');
    }
    public function Stamp($student_id = 0, $recordidStr = ''){
            $recordidStr = trim($recordidStr, ',');
            $recordidArr = explode(',', $recordidStr);
            if ($recordidArr[0] ==='') {
                $this->error('至少选择一个交费项进行打印!');
            }
            $recordArr = D('Record')->where(['id'=>['IN', $recordidArr]])->select();
            $this->assign('recordArr', $recordArr);
            $student = D('Student')->find($student_id);
            $this->assign('student',$student);
             $class=D('class')->where(['id'=>$student['class_id']])->find();
            $this->assign('class',$class);
            $grade=D('grade')->where(['id'=>$class['grade_id']])->find();
            $this->assign('grade',$grade);
            $major=D('major')->where(['id'=>$class['major_id']])->find();
            $this->assign('major',$major);
            $this->display('stamp');
    }
    public function RecordO($update_timeStart ='',$update_timeEnd ='',$water = '',$way='') {
        //查询信息
         $findFlag = 0;
        

        if (($water!= '')&&($findFlag == 0)) {
            $findMap['water'] = ['LIKE', '%'.$water.'%'];
        }
        if (($way!= '')&&($findFlag == 0)) {
            $findMap['way'] = ['LIKE', '%'.$way.'%'];
        }
        if($update_timeStart !='' && $update_timeEnd !=''){
            $findMap['time'] = ['between', [strtotime($update_timeStart), strtotime($update_timeEnd)]];
        }
        $model = M('Fee');
        $list = $model->where($findMap)->order('id')->limit($page->firstRow.','.$page->listRows)->select();
        $xlsName = "缴费记录";
        
            $xlsCell = [
                ["id","ID"],
                ["student_id","学生姓名"], 
                ["term_id","学期及交费项"],
                ["money","交费金额"],
                ["name","缴款经办人"],
                ["water","流水号"],
                ["time","缴费时间"],
            ];
        $model = M('Record');
        $xlsData = $model->where($findMap)->field()->order('id')->select();
          foreach ($xlsData as $k=>$v){
            $student=D('student')->where(['id'=>$v['student_id']])->find();
            $class=D('Class')->where(['id'=>$student['class_id']])->find();
            $grade=D('grade')->where(['id'=>$class['grade_id']])->find();
            $major=D('major')->where(['id'=>$class['major_id']])->find();
            $term=D('term')->where(['id'=>$v['term_id']])->find();
            $tuition=D('tuition')->where(['grade_id'=>$grade['id'], 'major'=>$major['id'], 'term_id'=>$term['id']])->find();
            unset($map);
            $map['id'] =$v['id'];
            $map['student_id'] =$student['name'];
            $map['term_id'] =  $term['name'];
            $map['money'] = $v['money'];
            $map['name'] = $v['name'];
            $map['water'] = "'".$v['water'];
            $map['time'] =date('Y-m-d',$v['time']);
            $newData[]=$map;
        }
        \exportExcel($xlsName, $xlsCell, $newData);
    }



}
